package com.qding.park.manager.sms.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.http.SimpleHttpParam;
import com.qding.park.common.utils.http.SimpleHttpResult;
import com.qding.park.common.utils.http.SimpleHttpUtils;
import com.qding.park.manager.pojo.sms.ExpireNotice;
import com.qding.park.manager.pojo.sms.ReserveDepositNotice;
import com.qding.park.manager.pojo.sms.SmsResult;
import com.qding.park.manager.pojo.sms.device.OutCheckAlarm;
import com.qding.park.manager.sms.SmsInterfaceServiec;
import com.qding.park.manager.sms.config.IMUrlConfig;

@Service("smsInterfaceServiec")
public class SmsInterfaceServiecImpl implements SmsInterfaceServiec {
	private static final Log _log = LogFactory.getLog(SmsInterfaceServiecImpl.class);
	/**
	  * @Fields url : TODO（短信地址）
	  */
	private final String url=IMUrlConfig.getSmsUrl();
	
	@Override
	public SmsResult reserve_deposit_notice(ReserveDepositNotice notice) {
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
		JSONObject  paramJson=new JSONObject();
		paramJson.put("mobile", notice.getMobile());
		String content="您预定的车位因超时未进场,系统已自动取消您的车位";
		paramJson.put("content", content);
		Map<String,Object> parameters = new LinkedHashMap<String,Object>();
		parameters.put("body", paramJson.toJSONString());
		httpParam.setParameters(parameters);
		_log.info("url:"+url);
		_log.info("httpParam:"+parameters);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		
		int code=result.getStatusCode();
		SmsResult smsResult=new SmsResult();
//		if(code!=200){
//			smsResult.setCode(code);
//			smsResult.setMessage(result.getExceptionMsg());
//		}else{
//			
//		}
		smsResult.setCode(code);
		smsResult.setMessage(result.getExceptionMsg());
		_log.info("return msg="+code);
		_log.info("return code="+smsResult.getCode());
		return smsResult;
	}

	@Override
	public SmsResult reserve_timeout_notice(String mobile) {
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
		JSONObject  paramJson=new JSONObject();
		paramJson.put("mobile", mobile);
		String content="因您未在约定时间内进场，系统已取消您的预定车位";
		paramJson.put("content", content);
		Map<String,Object> parameters = new LinkedHashMap<String,Object>();
		parameters.put("body", paramJson.toJSONString());
//		parameters.put("mobile", mobile);
//		parameters.put("content", content);
		httpParam.setParameters(parameters);
		_log.info("url:"+url);
		_log.info("httpParam:"+parameters);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		
		int code=result.getStatusCode();
		SmsResult smsResult=new SmsResult();
//		if(code!=200){
//			smsResult.setCode(code);
//			smsResult.setMessage(result.getExceptionMsg());
//		}else{
//			
//		}
		_log.info("return msg="+code);
		_log.info("return code="+smsResult.getCode());
		smsResult.setCode(code);
		smsResult.setMessage(result.getExceptionMsg());
		return smsResult;
	}

	@Override
	public SmsResult lockcar_checkout_alarm(OutCheckAlarm alarm) {
		_log.info("检测到您的" + alarm.getCarNum() + "车辆有出场行为，请及时确认。");
		_log.info(alarm.getMobile());
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
		JSONObject  paramJson=new JSONObject();
		paramJson.put("mobile", alarm.getMobile());
		String content="检测到您的" + alarm.getCarNum() + "车辆有出场行为，请及时确认。";
		paramJson.put("content", content);
		Map<String,Object> parameters = new LinkedHashMap<String,Object>();
		parameters.put("body", paramJson.toJSONString());
		httpParam.setParameters(parameters);
		_log.info("url:"+url);
		_log.info("httpParam:"+parameters);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		
		int code=result.getStatusCode();
		SmsResult smsResult=new SmsResult();
		smsResult.setCode(code);
		smsResult.setMessage(result.getExceptionMsg());
		_log.info("return msg="+code);
		_log.info("return code="+smsResult.getCode());
		return smsResult;
	}

	@Override
	public SmsResult expire_notice(ExpireNotice notice) {
		String content="您的" + notice.getCarNum() + "将于"+DateUtils.formatDate(notice.getExpireNotice(), DateUtils.DATE_FORMAT_DATEONLY)+"号到期，请及时续费。";
		_log.info(content);
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
		JSONObject  paramJson=new JSONObject();
		paramJson.put("mobile", notice.getMobile());
		
		paramJson.put("content", content);
		Map<String,Object> parameters = new LinkedHashMap<String,Object>();
		parameters.put("body", paramJson.toJSONString());
		httpParam.setParameters(parameters);
		_log.info("url:"+url);
		_log.info("httpParam:"+parameters);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		
		int code=result.getStatusCode();
		SmsResult smsResult=new SmsResult();
		smsResult.setCode(code);
		smsResult.setMessage(result.getExceptionMsg());
		_log.info("return msg="+code);
		_log.info("return code="+smsResult.getCode());
		return smsResult;
	}

}
